package com.itboye.mengwu.luntan.comment.adapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

import com.itboye.config.GlobalConfig;
import com.itboye.core.utils.TimesUtils;
import com.itboye.core.utils.XImageLoader;
import com.itboye.mengwu.R;
import com.itboye.mengwu.data.model.MePingLunbean;
import com.itboye.mengwu.data.model.MePingLunbean.MyPingLun;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MepingLunAdapter extends BaseAdapter {

	Context context;
	ArrayList<MePingLunbean.MyPingLun> list;

	public MepingLunAdapter(Context context,
			ArrayList<MePingLunbean.MyPingLun> list) {
		this.context = context;
		this.list = list;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list == null ? 0 : list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHouder houder;
		if (convertView == null) {
			houder = new ViewHouder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.item_mepinglun, null);
			houder.tv_title = (TextView) convertView
					.findViewById(R.id.tv_title);
			houder.textTime = (TextView) convertView
					.findViewById(R.id.textTime);
			houder.tv_content = (TextView) convertView
					.findViewById(R.id.tv_content);
			houder.iv_avatar = (ImageView) convertView
					.findViewById(R.id.iv_avatar);
			houder.txtPingLunTitle = (TextView) convertView
					.findViewById(R.id.txtPingLunTitle);
			houder.txtPinglunCountext = (TextView) convertView
					.findViewById(R.id.txtPinglunCountext);
			houder.tetPing = (TextView) convertView.findViewById(R.id.tetPing);
			houder.txtcome = (TextView) convertView.findViewById(R.id.txtcome);
			convertView.setTag(houder);

		} else {
			houder = (ViewHouder) convertView.getTag();
		}
		// XImageLoader.load(GlobalConfig.API_URL + "/picture/index?id="
		// + list.get(position).getHead(), houder.iv_avatar);
		if (list.get(position).getHead() == null
				|| "".equals(list.get(position).getHead())) {
			if ("1".equals(list.get(position).getSex())) {
				// imageView.setImageResource(R.drawable.male);
				houder.iv_avatar.setBackgroundResource(R.drawable.male);
			} else {
				houder.iv_avatar.setImageResource(R.drawable.female);
			}
		} else {
			if (!list.get(position).getHead().startsWith("http")) {
				XImageLoader.load(GlobalConfig.API_URL + "/picture/index?id="
						+ list.get(position).getHead(), houder.iv_avatar);
			} else {
				XImageLoader.load(list.get(position).getHead(),
						houder.iv_avatar);
			}
		}
		houder.tv_title.setText(list.get(position).getAuthor());
		houder.tv_content.setText(list.get(position).getMessage());
		houder.textTime
				.setText(new SimpleDateFormat("yy-MM-dd HH:mm:ss").format(Long
						.parseLong(list.get(position).getDateline()) * 1000));
		houder.txtPingLunTitle.setText(list.get(position).getThread_info()
				.getNickname());
		houder.txtPinglunCountext.setText(list.get(position).getThread_info()
				.getSubject());

		if (list.get(position).getThread_info().getFid().equals("4")) {
			houder.txtcome.setText("宝宝乐");
		} else if (list.get(position).getThread_info().getFid().equals("5")) {
			houder.txtcome.setText("妈妈赠");
		} else if (list.get(position).getThread_info().getFid().equals("6")
				|| list.get(position).getThread_info().getFid().equals("7")
				|| list.get(position).getThread_info().getFid().equals("8")) {
			houder.txtcome.setText("育儿经");
		} else if (list.get(position).getThread_info().getFid().equals("9")) {
			houder.txtcome.setText("时光机");
		}

		houder.tetPing.setTag(R.id.tag_first, position);
		houder.tetPing.setTag(R.id.tag_second, position);
		// houder.tetPing.setTag(R.id.tag_first,list.get(position));
		return convertView;
	}

	class ViewHouder {
		private TextView tv_title;
		private TextView textTime;
		private TextView tv_content;
		private TextView Guanzhu;

		private ImageView iv_avatar;
		// 回复的
		private TextView txtPingLunTitle;
		private TextView txtPinglunCountext;

		private TextView tetPing;
		// 来自哪里
		private TextView txtcome;

	}

}
